{************************************************}
{                                                }
{   ObjectWindows Demo                           }
{   Copyright (c) 1992 by Borland International  }
{                                                }
{************************************************}

unit wprint;

interface

uses Objects, WinTypes, WinProcs, Strings,
     OWindows, OStdDlgs, OPrinter,
     CARTOCmds, CartoDlg,
     tit_win, get_win, gmuf_win, aux_win, outl_win, coul_win;

type

  PTextPrint = ^TTextPrint;
  TTextPrint = object(TPrintout)
    TextHeight, LinesPerPage, FirstOnPage, LastOnPage: Integer;
    TheLines: PCollection;
    constructor Init(ATitle: string; TheText: PLineCollection);
    function GetDialogInfo(var Pages: Integer): Boolean; virtual;
    function HasNextPage(Page: Word): Boolean; virtual;
    procedure SetPrintParams(ADC: HDC; ASize: TPoint); virtual;
    procedure PrintPage(Page: Word; var Rect: TRect; Flags: Word); virtual;
  end;

Implementation

constructor TTextPrint.Init(ATitle: String; TheText: PLineCollection);
begin
  StrPcopy(MessageText,ATitle);
  inherited Init(MessageText);
  TheLines := TheText;
  FirstOnPage := 0;
  LastOnPage := 0;
end;

function TTextPrint.GetDialogInfo(var Pages: Integer): Boolean;
begin
  Pages := TheLines^.Count div LinesPerPage + 1;
  GetDialogInfo := True;
end;

function TTextPrint.HasNextPage(Page: Word): Boolean;
begin
  HasNextPage := LastOnPage < TheLines^.Count - 1;
end;

procedure TTextPrint.SetPrintParams(ADC: HDC; ASize: TPoint);
var
  TextMetrics: TTextMetric;
begin
  inherited SetPrintParams(ADC, ASize);
  GetTextMetrics(DC, TextMetrics);
  TextHeight := TextMetrics.tmHeight;
  LinesPerPage := Size.Y div TextHeight;
end;

procedure TTextPrint.PrintPage(Page: Word; var Rect: TRect; Flags: Word);
var
  Line: Integer;
  TheText: PString;
begin
  FirstOnPage := (Page - 1) * LinesPerPage;
  LastOnPage := (Page * LinesPerPage) - 1;
  if LastOnPage >= TheLines^.Count then LastOnPage := TheLines^.Count - 1;
  for Line := FirstOnPage to LastOnPage do
  begin
    TheText := TheLines^.At(Line);
    if Length(TheText^) = 0 then StrPCopy(MessageText, ' ')
      else StrPcopy(MessageText, TheText^);

    if TheText <> nil then
      TextOut(DC, 0, (Line - FirstOnPage)*TextHeight, Messagetext, StrLen(MessageText));
  end;
end;

end.
